package praktikum_2_ylesanded.ylesanne_2;

import helperClasses.FileHelper;
import praktikum_2_ylesanded.ylesanne_1.PriorityQueue;
import praktikum_2_ylesanded.ylesanne_1.Stack;

/**
 * Created by IntelliJ IDEA.
 * User: Jaan Sepp and Martti Elias
 * Date: 21.10.11
 * Time: 12:12
 */
public class TestHelper {

  FileHelper fileHelper = new FileHelper();
  Stack stack = new Stack();
  PriorityQueue priorityQueue = new PriorityQueue();

  public int[] getAndReadIntegersFromFile(String fileName){
    String contents = fileHelper.readFromFile(fileName);
    try{
      String[] numbers = contents.split("\n");
      int[] numbersFromFile = new int[numbers.length];
      for (int i = 0; i < numbers.length; i++) {
        numbersFromFile[i] = Integer.valueOf(numbers[i]);
      }
      return numbersFromFile;
    } catch(Exception e){
      System.out.println("ERROR: File contents includes non-integer values!");
    }
    return null;
  }

  public void processNumbers(int[] numbers) {
    String contentsToWriteFromMagasin = "";
    String contentsToWriteFromPriorityQueue = "";
    for(int number : numbers) {
        if(number > 0) {
          stack.push(number);
          priorityQueue.enqueue(number);
        }
        if(number == 0) {
          while (!stack.isEmpty()){
            contentsToWriteFromMagasin += stack.pop() + "\n";
          }
          while (!priorityQueue.isEmpty()) {
            contentsToWriteFromPriorityQueue += priorityQueue.dequeue() + "\n";
          }
        }
      if(number < 0){
        number = -number;
        int tempNumber = number;
        while(number > 0 && !stack.isEmpty()) {
          contentsToWriteFromMagasin += stack.pop() + "\n";
          number --;
        }
        number = tempNumber;
        while(number > 0 && !priorityQueue.isEmpty()) {
          contentsToWriteFromPriorityQueue += priorityQueue.dequeue()+ "\n";
          number --;
        }
      }
    }
    fileHelper.writeToFile("output-mag3", contentsToWriteFromMagasin);
    fileHelper.writeToFile("output-pri3", contentsToWriteFromPriorityQueue);
  }

}
